package com.mane.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mane.entity.DTO.LoginDTO;
import com.mane.mapper.UserMapper;
import com.mane.pojo.User;
import com.mane.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public User login(LoginDTO loginDTO) {
        //先查名称
        User user = userMapper.selectUserByUsername(loginDTO.getUsername());
        if(user == null){
            return null;
        }
        //比较密码
        BCryptPasswordEncoder bCryptPasswordEncoder = new BCryptPasswordEncoder();
        System.out.println(bCryptPasswordEncoder.encode(loginDTO.getPassword()));
        boolean matches = bCryptPasswordEncoder.matches(loginDTO.getPassword(), user.getPassword());
        if(!matches){
            return null;
        }
        return user;
    }

    @Override
    public User selectUserByUsername(String username) {
        User user = userMapper.selectUserByUsername(username);
        return user;
    }


}
